package com.zhongwang.mms.module.half.controller;

import com.zhongwang.mms.base.BaseController;
import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.gen.entity.HInventoryProjectMark;
import com.zhongwang.mms.gen.entity.User;
import com.zhongwang.mms.module.half.model.HInventoryProjectMarkModel;
import com.zhongwang.mms.module.half.model.HInventoryQueryModel;
import com.zhongwang.mms.module.half.service.InvToProMarkService;
import com.zhongwang.mms.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @description:库存划项目标记
 * @author: th
 * @time: 2020/5/7 8:22
 */
@Controller
@RequestMapping("/half/halInvManage/invToProMark")
public class InvToProMarkController extends BaseController {

    //页面文件路径
    private String prefix = "half/halInvManage/invToProMark";
    @Autowired
    private InvToProMarkService invToProMarkService;

    /**
     * 加载页面
     *
     * @param model
     * @return
     */
    @GetMapping("/page")
    @RequiresPermissions({"halinv:halwarpro:view"})
    public String create(Model model) {

        // 获取登入用户实体
        User user = ShiroUtils.getUser();
        model.addAttribute("userName", user.getName());
        model.addAttribute("odrDate", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        return prefix + "/page";
    }

    /**
     * 跳转新增标记页面
     * @param model
     * @return
     */
    @GetMapping("/createPage")
    @RequiresPermissions({"halinv:halwarpro:view"})
    public String createPage(Model model) {

        // 获取登入用户实体
        User user = ShiroUtils.getUser();
        model.addAttribute("userName", user.getName());
        model.addAttribute("odrDate", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        return prefix + "/createPage";
    }

    /**
     * 加载页面数据
     * @param query
     * @return
     */
    @ResponseBody
    @PostMapping("/list")
    @RequiresPermissions({"halinv:halwarpro:view"})
    public JsonResult list(HInventoryProjectMarkModel query) {

        startPage();
        return jsonResult(invToProMarkService.selectHInventoryProjectMarkByQuery(query));
    }

    /**
     * 新增标记加载数据
     * @param query
     * @return
     */
    @ResponseBody
    @PostMapping("/inventoryList")
    @RequiresPermissions({"halinv:halwarpro:view"})
    public JsonResult inventoryList(HInventoryQueryModel query) {

        startPage();
        return jsonResult(invToProMarkService.getInventoryList(query));
    }

    /**
     * 保存标记申请
     * @param hInventoryProjectMark
     * @return
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions({"halinv:halwarpro:edit"})
    public JsonResult save(HInventoryProjectMark hInventoryProjectMark) {

        if (invToProMarkService.saveInvToPro(hInventoryProjectMark)) {
            return JsonResult.success();
        }
        return JsonResult.fail();
    }

    /**
     * 提交标记申请
     * @param hInventoryProjectMarkModel
     * @return
     */
    @ResponseBody
    @PostMapping("/submitApply")
    @RequiresPermissions({"halinv:halwarpro:edit"})
    public JsonResult submitApply(HInventoryProjectMarkModel hInventoryProjectMarkModel) {

        if (invToProMarkService.submitApply(hInventoryProjectMarkModel)) {
            return JsonResult.success();
        }
        return JsonResult.fail();
    }

    /**
     * 删除标记申请
     * @param hInventoryProjectMarkModel
     * @return
     */
    @ResponseBody
    @PostMapping("/removeApply")
    @RequiresPermissions({"halinv:halwarproexport:del"})
    public JsonResult removeApply(HInventoryProjectMarkModel hInventoryProjectMarkModel) {

        if (invToProMarkService.removeApply(hInventoryProjectMarkModel)) {
            return JsonResult.success();
        }
        return JsonResult.fail();
    }

    /**
     * 保存修改
     * @param hInventoryProjectMarkModel
     * @return
     */
    @ResponseBody
    @PostMapping("/update")
    @RequiresPermissions({"halinv:halwarpro:edit"})
    public JsonResult update(HInventoryProjectMarkModel hInventoryProjectMarkModel) {

        if (invToProMarkService.update(hInventoryProjectMarkModel)) {
            return JsonResult.success();
        }
        return JsonResult.fail();
    }

    /**
     * 库存划项目标记导入 页面
     *
     * @return 页面路径
     */
    @GetMapping("/import")
    @RequiresPermissions({"halinv:halwarproexport:add"})
    public String importMark() {

        return prefix +"/import";
    }

    /**
     * 导出待标记库存在库
     *
     * @param response
     */
    @GetMapping("/inventoryExport")
    @RequiresPermissions({"halinv:halwarproexport:add"})
    public void inventoryExport(HttpServletResponse response) {
        // 执行导出Excel方法
        invToProMarkService.inventoryExport(response);
    }

    /**
     * 库存划项目excel导入执行
     *
     * @param file 导入文件
     * @return JsonResult
     */
    @ResponseBody
    @PostMapping("/importFile")
    @RequiresPermissions({"halinv:halwarproexport:add"})
    public JsonResult importFile(@RequestParam("file") MultipartFile file, HttpServletResponse response) {
        invToProMarkService.deleteFileByUserId(ShiroUtils.getUserId());
        String msg = invToProMarkService.importFile(file);
        JsonResult jsonResult;
        if ("导入失败".equals(msg)) {
            jsonResult = error();
        } else {
            jsonResult = success();
        }
        jsonResult.setMsg(msg);
        return jsonResult;
    }

    /**
     * 导出标记导入异常Excel(15天之内)
     *
     * @param response
     */
    @GetMapping("/errorExport")
    @RequiresPermissions({"halinv:halwarproexport:add"})
    public void errorExport(HttpServletResponse response) {
        invToProMarkService.exportErrorExport(response);
    }
}
